package middleware

import (
	"fmt"
	"github.com/gin-gonic/gin"
	"liuche17.cn/go-blog/server/global"
	"liuche17.cn/go-blog/server/model/common/resp"
)

func CustomRecovery() gin.HandlerFunc {
	return func(c *gin.Context) {
		defer func() {
			if r := recover(); r != nil {
				// 记录错误日志
				errInfo := fmt.Sprintf("Recovered from panic: %v", r)
				global.GB_LOG.Error(errInfo)
				// 统一响应
				resp.FailWithMessage("系统开小差啦", c)
				c.Abort() // 终止请求
			}
		}()
		c.Next() // 执行下一个中间件/处理函数
	}
}
